layui.define(['treeTable', 'form', 'admin', 'table', 'element'], function(exports){
  var $ = layui.$,form = layui.form,treeTable = layui.treeTable,table = layui.table,element = layui.element;

  treeTable.render({
      id:'LAY-tree-list'
      ,elem: '#LAY-tree-list'
      ,tree: {
          iconIndex: 2,           // 折叠图标显示在第几列
          isPidData: true,        // 是否是id、pid形式数据
          idName: 'id',  			// id字段名称
          pidName: 'parentId'     // pid字段名称
      },
      cols: [[
          {title: '操作', width: 260, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
          ,{field: 'id', width: 100, title: 'ID'}
          ,{field: 'name', title: '商品类型', minWidth: 100}
          ,{field: 'code', title: '类型Code'}
          ,{field: 'classifyName', width: 100, title: '分类'}
          ,{field: 'priority', title: '优先级'}
          ,{field: 'stateName',title: '状态'}
          ,{field: 'createDate', title: '创建时间'}
      ]],
      reqData: function(data, callback) {
          var parentId = null;
          if(data != null){parentId = data.id;}
          $.get(layui.setter.webRoot + '/market/cloud/goods/type/tree/list.data',{
              parentId:parentId
          },function (res) {
              if(res.code==200){
                  callback(res.body.data);
              } else {
                  callback(res.msg)
              };
          });
      }
  });

    table.render({
        id:'LAY-table'
        ,elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/market/cloud/goods/type/list.data'
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'id', width: 100, title: 'ID'}
            ,{field: 'name', title: '商品类型', minWidth: 100}
            ,{field: 'code', title: '类型Code'}
            ,{field: 'classifyName', title: '分类'}
            ,{field: 'priority', title: '优先级'}
            ,{field: 'stateName',title: '状态'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 260, align:'center',  toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: true
        ,height: 'full'
    });

    element.on('tab(tableTab)', function(data){
        if(data.index == 2){
            table.reload('LAY-table');
        }
    });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
      element.tabChange('tableTab','generalTable');
      var field = data.field;
      //执行重载
      table.reload('LAY-table', {
          where: field
          ,page: {curr:1}
      });
  });
  
  //事件
  var active = {
    add: function(){
      layer.open({
        type: 2
        ,title: '新增商品类型'
        ,content: layui.setter.webRoot + '/market/cloud/goods/type/add.html'
        ,maxmin: true
        ,area: ['70%', '400px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-form-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);

          //监听提交
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field; //获取提交的字段
        	$.ajax({
	    		type: "post",
	            url: layui.setter.webRoot + '/market/cloud/goods/type/add.do',
	            data:field,
				dataType:"json",
				success:function(res){
					if(res.code == 200){
                        table.reload('LAY-table');
                        treeTable.reload('LAY-tree-list');
						layer.close(index); //关闭弹层
					}else{
						layer.msg(res.msg,{icon: 2});
					}
				}
	        });
          });  
          submit.trigger('click');
        }
      }); 
    }
    ,addItem: function(obj){
          layer.open({
              type: 2
              ,title: '新增商品类型'
              ,content: layui.setter.webRoot + '/market/cloud/goods/type/item/add.html?parentId='+obj.data.id
              ,maxmin: true
              ,area: ['70%', '400px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);

                  //监听提交
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field; //获取提交的字段
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/market/cloud/goods/type/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  treeTable.reload('LAY-tree-list');
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,update:function(obj){
    	layer.open({
            type: 2
            ,title: '编辑商品类型'
            ,content: layui.setter.webRoot + '/market/cloud/goods/type/update.html?id='+obj.data.id
            ,maxmin: true
            ,area: ['70%', '400px']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
              var iframeWindow = window['layui-layer-iframe'+ index]
              ,submitID = 'LAY-form-submit'
              ,submit = layero.find('iframe').contents().find('#'+ submitID);

              iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                var field = data.field; //获取提交的字段
                $.ajax({
    	    		type: "post",
    	            url: layui.setter.webRoot + '/market/cloud/goods/type/update.do',
    	            data:field,
    				dataType:"json",
    				success:function(res){
    					if(res.code == 200){
                            table.reload('LAY-table');
                            treeTable.reload('LAY-tree-list');
    						layer.close(index); //关闭弹层
    					}else{
    						layer.msg(res.msg,{icon: 2});
    					}
    				}
    	        });
              });  
              
              submit.trigger('click');
            }
          });
    }
    ,del:function(obj){
    	layer.confirm('确定要删除商品类型吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/market/cloud/goods/type/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
                    table.reload('LAY-table');
                    treeTable.reload('LAY-tree-list');
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.layui-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });

    treeTable.on('tool(LAY-tree-list)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
  
  exports('goods_type_list', {})
});